package com.hhs.storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hhs.storage.bean.ProjectMember;
import com.hhs.storage.bean.ProjectWorkFlow;
import com.hhs.storage.bean.ResponseInfo;
import com.hhs.storage.bean.SubWkFlow;
import com.hhs.storage.bean.dto.WorkFlow;
import com.hhs.storage.dao.ProjectWorkFlowDao;
import com.hhs.storage.service.ProjectMemberService;
import com.hhs.storage.service.ProjectPlanService;
import com.hhs.storage.service.ProjectWorkFlowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhs.storage.util.StringUtil;
import com.hhs.storage.util.SysUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hhs
 * @since 2023-02-10
 */
@Service
@Transactional
public class ProjectWorkFlowServiceImpl extends ServiceImpl<ProjectWorkFlowDao, ProjectWorkFlow> implements ProjectWorkFlowService {
    @Resource
    private ProjectPlanService projectPlanService;
    @Resource
    ProjectMemberService projectMemberService;

    @Override
    public Map<String, Object> getAll(ProjectWorkFlow projectWorkFlow) {
        QueryWrapper<ProjectWorkFlow> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(projectWorkFlow.getProjectPlanId())) {
            queryWrapper.eq("project_plan_id", projectWorkFlow.getProjectPlanId());
        }
        queryWrapper.orderByAsc("step");
        List<ProjectWorkFlow> list = this.list(queryWrapper);
        Map map = new HashMap<String, Object>();
        map.put("rows", list);
        map.put("code", 1);
        return map;
    }

    @Override
    public Map<String, Object> add(Integer step, String content, String projectPlanId) {
        if (!projectPlanService.isProjectHeader(projectPlanId)) {
            return ResponseInfo.error("项目负责人才可新增工序");
        }
        ProjectWorkFlow projectWorkFlow = new ProjectWorkFlow();
        projectWorkFlow.setId(SysUtil.UUID());
        projectWorkFlow.setStep(step);
        projectWorkFlow.setContent(content);
        projectWorkFlow.setProjectPlanId(projectPlanId);
        projectWorkFlow.setStatus(ProjectWorkFlow.STATUS_CREATE);
        projectWorkFlow.setCreateTime(LocalDateTime.now());
        this.save(projectWorkFlow);
        return ResponseInfo.success();
    }

    @Override
    public Map<String, Object> edit(Integer step, String content, String id) {

        ProjectWorkFlow projectWorkFlow = this.getById(id);
        if (projectWorkFlow == null) {
            return ResponseInfo.error("错误的参数");
        }
        if (!projectPlanService.isProjectHeader(projectWorkFlow.getProjectPlanId())) {
            return ResponseInfo.error("项目负责人才可编辑工序");
        }
        projectWorkFlow.setStep(step);
        projectWorkFlow.setContent(content);
        this.updateById(projectWorkFlow);
        return ResponseInfo.success();
    }

    @Override
    public Map<String, Object> dele(String id) {
        ProjectWorkFlow projectWorkFlow = this.getById(id);
        if (projectWorkFlow == null) {
            return ResponseInfo.error("错误的参数");
        }
        if (!projectPlanService.isProjectHeader(projectWorkFlow.getProjectPlanId())) {
            return ResponseInfo.error("项目负责人才可删除工序");
        }
        this.removeById(id);
        return ResponseInfo.success();
    }

    @Override
    public Map<String, Object> changeStatue(String id, Integer status) {
        ProjectWorkFlow projectWorkFlow = this.getById(id);
        if (projectWorkFlow == null) {
            return ResponseInfo.error("错误的参数");
        }
        if (SysUtil.PROJECT_WORKFLOW_STATUS_MAP.get(status) == null) {
            return ResponseInfo.error("错误的参数");
        }
        if (projectWorkFlow == null) {
            return ResponseInfo.error("错误的参数");
        }
        if (!projectPlanService.isProjectHeader(projectWorkFlow.getProjectPlanId())) {
            return ResponseInfo.error("项目负责人才可更改工序状态");
        }
        UpdateWrapper<ProjectWorkFlow> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        updateWrapper.set("status", status);
        LocalDateTime complete_time = null;
        if (status == ProjectWorkFlow.STATUS_CREATE) {
            complete_time = null;
        } else if (status == ProjectWorkFlow.STATUS_COMPLETE) {
            complete_time = LocalDateTime.now();
        }
        updateWrapper.set("complete_time", complete_time);

        this.update(updateWrapper);
        //更新其他的工序状态
//        updateWrapper=new UpdateWrapper<>();
//        updateWrapper.eq("project_plan_id",projectWorkFlow.getProjectPlanId());//项目计划id
//        if (status==ProjectWorkFlow.STATUS_CREATE){//设置未完成，之后的工序都未完成
//            updateWrapper.ge("step",projectWorkFlow.getStep());
//            complete_time=null;
//        }else if (status==ProjectWorkFlow.STATUS_COMPLETE){//设置完成，之前的工序都完成
//            complete_time=LocalDateTime.now();
//            updateWrapper.le("step",projectWorkFlow.getStep());
//        }
//        updateWrapper.set("status",status);
//        updateWrapper.set("complete_time",complete_time);
//        this.update(updateWrapper);
        return ResponseInfo.success();
    }

    @Override
    public void addWorkFlow(String projectPlanId, List<SubWkFlow> projectWorkFlows, Integer type) {
        int rowindex=0;
        for (SubWkFlow swf:projectWorkFlows){
                for (int i = 0; i < swf.getTableData().size(); i++) {
                    ProjectWorkFlow member = swf.getTableData().get(i);
                    if (org.apache.commons.lang3.StringUtils.isEmpty(member.getContent())) {
                        throw new RuntimeException("工序内容不可为空");
                    }
                    if(StringUtil.iszNull(member.getId())){
                        member.setId(SysUtil.UUID());
                    }
                    member.setStep(i);
                    member.setProjectPlanId(projectPlanId);
                    member.setStatus(member.getStatus());
                    member.setCreateTime(LocalDateTime.now());
                    member.setType(type);
                    member.setRowIndex(rowindex);
                }
                this.saveOrUpdateBatch(swf.getTableData());
                rowindex++;
        }

    }

    @Override
    public void deleByPlanId(String id) {
        if (StringUtils.isNotEmpty(id)) {
            QueryWrapper<ProjectWorkFlow> qw = new QueryWrapper<>();
            qw.eq("project_plan_id", id);
            this.baseMapper.delete(qw);
        }
    }

    @Override
    public Map<String, Object> wkEdit(WorkFlow workFlow) {
//        if (workFlow.getId() == null) {
//            return ResponseInfo.error("参数错误");
//        }
//        addWorkFlow(workFlow.getId() + "", workFlow.getSubWk(), workFlow.getType());
//
//        //项目计划成员工序，提交后要修改进度
//        if (workFlow.getType() != null && workFlow.getType().equals(5)) {
//            String[] idArr = workFlow.getId().split("-");
//            if (workFlow.getSubWk().size() > 0) {
//                float completeNum = 0f;
//                for (ProjectWorkFlow projectWorkFlow : workFlow.getSubWk()) {
//                    if (projectWorkFlow.getStatus().equals(1)) {
//                        completeNum += 1;
//                    }
//                }
//                int progress = (int) ((completeNum / workFlow.getSubWk().size()) * 100);
//
//                UpdateWrapper<ProjectMember> qw = new UpdateWrapper<>();
//                qw.eq("project_id", idArr[1]);
//                qw.eq("user_id", idArr[0]);
//                qw.set("progress", progress);
//                qw.set("wk_flag", 1);
//                projectMemberService.update(qw);
//            } else {
//                UpdateWrapper<ProjectMember> qw = new UpdateWrapper<>();
//                qw.eq("project_id", idArr[1]);
//                qw.eq("user_id", idArr[0]);
//                qw.set("progress", 0);
//                qw.set("wk_flag", 0);
//                projectMemberService.update(qw);
//            }
//        }

        return ResponseInfo.success();
    }

    @Override
    public Map<String, Object> wkQuery(WorkFlow workFlow) {
        if (workFlow.getId() == null) {
            return ResponseInfo.error("参数错误");
        }
        QueryWrapper qw3 = new QueryWrapper<ProjectWorkFlow>();
        qw3.eq("project_plan_id", workFlow.getId());
        if(workFlow.getType()==null){
            qw3.isNull("type");
        }else {
            qw3.eq("type", workFlow.getType());
        }
        qw3.orderByAsc("step");
        workFlow.setSubWk(list(qw3));
        return ResponseInfo.success(workFlow);
    }

}
